<?php

namespace App\Admin\Controllers\Doctor;

use App\Models\Doctor\Doctor;
use App\Base\BaseAdminController;
use Encore\Admin\Form;
use Illuminate\Support\Facades\DB;
use Encore\Admin\Grid;
//use James\Admin\Grid;
use Encore\Admin\Facades\Admin;

class DoctorController extends BaseAdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '医生管理';

    protected $modelName = Doctor::class;

    const FIELDS = [
        "id"                     => "ID",
        "hash_id"                => "hash_id",
        "code"                   => "编码",
        "name"                   => "姓名",
        "mobile"                 => "手机号",
        "gender"                 => "性别",
        "avatar"                 => "头像",
        "birthday"               => "出生日期",
        "company"                => "单位",
        "department"             => "部门",
        "job"                    => "职称",
        "title"                  => "头衔",
        "description"            => "个人介绍",
        "specialty"              => "擅长领域",
        "occupation"             => "职业信息",
        "society"                => "社会职务",
        "honor"                  => "荣誉称号",
        "motto"                  => "医生寄语",
        "tag"                    => "标签",
        "recommend_num"          => "推荐数量",
        "recommend_real"         => "实际推荐",
        "source_type"            => "医生来源",
        "source_id"              => "来源ID",
        "is_test"                => "测试",
        "im_user_id"             => "IM ID",
        "im_user_token"          => "IM TOKEN",
        "medical_certificate"    => "医执医证",
        "seq"                    => "排序",
        "created_at"             => "创建时间",
        "updated_at"             => "创建时间",

        "im_status"              => "IM 状态",
        "user_count"             => "患者数",
        "recipe_count"           => "开方数",
        "recipe_order_count"     => "订单数",

    ];


    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new $this->modelName);

        $grid->filter(function ($filter) {

            $filter->like('code', self::FIELDS['code']);
            $filter->like('name', self::FIELDS['name']);
            $filter->like('mobile', self::FIELDS['mobile']);
        });

        $grid->column('id', self::FIELDS['id'])->sortable();
        $grid->column("avatar", self::FIELDS["avatar"])->lightbox(['width' => 20, 'height' => 30]);
        $grid->column('code', self::FIELDS['code'])->sortable();
        $grid->column('name', self::FIELDS['name'])->sortable();
        $grid->column('mobile', self::FIELDS['mobile'])->sortable();
        $grid->gender(self::FIELDS['gender'])->display(function($val) {
            $opt = [0 =>'保密', 1 => '男', 2 => '女'];
            return $opt[$val];
        })->sortable();
        $grid->column('company', self::FIELDS['company'])->sortable();
        $grid->column('department', self::FIELDS['department'])->sortable();

        $that = $this;
        $grid->column('', self::FIELDS["im_status"])->display(function () use($that){
            $imuserId = $this->im_user_id;
            if (empty($imuserId))
            {
                return "<p style='color:darkviolet'>ID为空</p>";
            }
            else
            {
                // $status = $that->service("Inq\RongCloud")->getUserInfo($imuserId);
                // if($status)
                // {
                //     if ($status['code'] == 200)
                //     {
                //         return '正常';
                //     }
                //     elseif ($status['code'] == 400)
                //     {
                //         return "<p style='color:red'>ID异常</p>";
                //     }
                //     else
                //     {
                //         return "<p style='color:blue'>未知错误</p>";
                //     }
                // }
                // else
                {
                    return "<p style='color:blue'>未知错误</p>";
                }
            }
        })->sortable();

        if (Admin::user()->isRole('tester'))
        {
            $grid->column('is_test',self::FIELDS['is_test'])->display(function($val) {
                if ($val > 0)
                {
                    return "<span style='color:red'>是</span>";
                }
                else
                {
                    return "否";
                }

            })->sortable();
        }

        // $grid->column("user_count", self::FIELDS["user_count"])->sortable();
        // $grid->column("recipe_count", self::FIELDS["recipe_count"])->sortable();
        // $grid->column("recipe_order_count", self::FIELDS["recipe_order_count"])->sortable();
        $grid->column("seq", self::FIELDS["seq"])->sortable();

        $grid->column('updated_at', self::FIELDS['updated_at'])->sortable();

        $this->setGridModel($grid->model());

        return $grid;
    }


    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new $this->modelName);

        $form->display('id', self::FIELDS['id']);
        $form->display("hash_id", self::FIELDS["hash_id"]);

        $form->text("code", self::FIELDS["code"])->required();
        $form->text("name", self::FIELDS["name"])->required();
        $form->text("mobile", self::FIELDS["mobile"])->required();

        $opt = [0 =>'保密', 1 => '男', 2 => '女'];
        $form->select('gender', self::FIELDS['gender'])->options($opt)->default(1)->required();;
        $form->date("birthday", self::FIELDS["birthday"])->required();
        $form->image('avatar', self::FIELDS['avatar']);

        $form->text("company", self::FIELDS["company"]);
        $form->text("department", self::FIELDS["department"]);
        $form->text("job", self::FIELDS["job"]);
        $form->text("title", self::FIELDS["title"]);
        $form->textarea("description", self::FIELDS["description"]);
        $form->text("specialty", self::FIELDS["specialty"]);
        $form->text("occupation", self::FIELDS["occupation"]);
        $form->text("society", self::FIELDS["society"]);
        $form->text("honor", self::FIELDS["honor"]);
        $form->text("motto", self::FIELDS["motto"]);
        $form->text("tag", self::FIELDS["tag"]);
        $form->text("recommend_num", self::FIELDS["recommend_num"]);
        $form->text("recommend_real", self::FIELDS["recommend_real"]);

        $form->text("source_type", self::FIELDS["source_type"]);
        $form->text("source_id", self::FIELDS["source_id"]);

        // 是否是测试医生
        $test_states = [
            'on'    => ['value' => 1, 'text' => '是', 'color' => 'success'],
            'off'   => ['value' => 0, 'text' => '否', 'color' => 'danger'],
        ];
        $form->switch('is_test', self::FIELDS["is_test"])->states($test_states)->default(0);
        $form->text("im_user_id", self::FIELDS["im_user_id"]);
        $form->text("im_user_token", self::FIELDS["im_user_token"]);
        $form->text("medical_certificate", self::FIELDS["medical_certificate"]);
        $form->text('seq', self::FIELDS["seq"]);

        $form->display("created_at", self::FIELDS["created_at"]);
        $form->display("updated_at", self::FIELDS["updated_at"]);

        return $form;
    }

    private function setGridModel($model)
    {
        // $model->select('doctor.*',
        //                DB::raw("(select count(1) from user_doctor where user_doctor.doctor_id=doctor.id) as user_count"),
        //                DB::raw("(select count(1) from user_doctor where user_doctor.doctor_id=doctor.id) as user_count"),
        //                DB::raw("(select count(1) from inq_recipe where inq_recipe.doctor_id=doctor.id) as recipe_count"),
        //                DB::raw("(select count(1) from inq_recipe_order join inq_recipe on inq_recipe.id=inq_recipe_order.recipe_id where inq_recipe_order.status>=20 and inq_recipe.doctor_id = doctor.id) as recipe_order_count")
        // );

        if (!Admin::user()->isRole('tester'))
        {
            $model->where('is_test','0');
        }
    }
}
